package com.network;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by kkmike999 on 2016/06/04.
 */
public class MyHttpClient {

	public String get(String path) throws IOException {
		return get(path, new HashMap<String, Object>());
	}

	public String get(String path, Map<String, Object> params) throws IOException {
		OkHttpClient client = new OkHttpClient();

		// "http://kkmike999-file.b0.upaiyun.com/" + path
		HttpUrl.Builder urlBuilder = new HttpUrl.Builder().scheme("http")
		                                                  .host("kkmike999-file.b0.upaiyun.com")
		                                                  .addPathSegment(path);

		for (String key : params.keySet()) {
			urlBuilder.addQueryParameter(key, params.get(key)
			                                        .toString());
		}

		HttpUrl httpUrl = urlBuilder.build();

		Request request = new Request.Builder().url(httpUrl)
		                                       .build();

		Response response = client.newCall(request)
		                          .execute();
		return response.body()
		               .string();
	}
}
